export function loadImage(param, onEach) {
  if (Array.isArray(param)) {
    var src = param[0];
    var weight = param[1];
  } else {
    var src = param;
    var weight = 1;
  }
  return new Promise(function (resolve, reject) {
    var img = new Image();
    img.src = src;
    img.onload = function () {
      onEach && onEach(weight);
      var src = this.getAttribute('src');
      resolve(src);
    };
    img.onerror = function () {
      var src = this.getAttribute('src');
      if (confirm(`图片${src}加载失败，是否重新加载？`)) {
        loadImage(param, onEach).then(resolve).catch(reject);
      } else {
        onEach && onEach(weight);
        resolve(src);
      }
    };
  });
}

export default function (srcs, onEach) {
  return Promise.all(srcs.map(function (src) {
    return loadImage(src, onEach);
  }));
}